//
//  CYBaseModel+CYExchange.m
//  SafetyInHand
//
//  Created by admin on 2022/7/27.
//

#import "CYBaseModel+CYExchange.h"

@implementation CYBaseModel (CYExchange)
/// 模型转字典
- (NSDictionary *)cy_toKeyValue {
    return [self mj_keyValues];
}

+ (instancetype)cy_modelFromKeyValue:(id)keyValue {
    return [self mj_objectWithKeyValues:keyValue];
}

+ (NSArray *)cy_keyValuesFromModels:(NSArray *)models {
    NSArray *array = [self mj_keyValuesArrayWithObjectArray:models];
    return array ? : [NSArray array];
}

+ (NSArray *)cy_modelsFromKeyValues:(NSArray *)keyValues {
    NSArray *array = [self mj_objectArrayWithKeyValuesArray:keyValues];
    return  array ? : [NSArray array];
}


- (NSString *)cy_toJson {
    NSString *result = nil;
    NSDictionary *dict = [self cy_toKeyValue];
    if ([NSJSONSerialization isValidJSONObject:dict]) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
        if (data) {
            result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        }
    }
    return result;
}


@end
